﻿/*

******************************************************

CreateDate  *  Author         *  Content

2008-8-1   *  donne.yang     *  DB operator

******************************************************

NO   *  UpdateDate   *  Author        *  Content

*********************************************************

*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using System.Collections;
using System.Configuration;

using FoxConn.Lib.Dbo;

namespace OracleToInformix.Source
{
    class DBOperation
    {
        private string strDBTYPE = "0";

        /// <summary>
        /// DB操作类
        /// </summary>
        /// <param name="intDBType">默认为Oracle。"0" - oracle db,"1" - Informix(odbc) db </param>
        public DBOperation(Int32 intDBType)
        {
            this.strDBTYPE = intDBType.ToString();
        }

        /// <summary>
        /// 查询数据,默认为Oracle数据库.
        /// </summary>
        /// <param name="strSQL">Sql语句</param>
        /// <returns>查询结果</returns>
        public DataSet GetResult(string strSQL)
        {
            DataSet ds = new DataSet();
            switch (strDBTYPE)
            {
                case "0":
                    OracleDBOperation oracleDBOperation = new OracleDBOperation();
                    ds = oracleDBOperation.GetResult(strSQL); break;
                case "1":
                    OdbcDBOpration odbcDBOpration = new OdbcDBOpration();
                    ds = odbcDBOpration.GetResult(strSQL); break;
                default:
                    OracleDBOperation oracleDBOperationDefault = new OracleDBOperation();
                    ds = oracleDBOperationDefault.GetResult(strSQL); break;
            }
            return ds;
        }

        /// <summary>
        /// 获取数据，默认为Oracle。"0" - oracle db,"1" - Informix(odbc) db 
        /// </summary>
        /// <param name="strSQL">SQL语句</param>
        /// <param name="strConn">DB连接字符串</param>
        /// <returns>DataSet</returns>
        public DataSet GetResult(string strSQL, string strConn)
        {
            DataSet ds = new DataSet();
            switch (strDBTYPE)
            {
                case "0":
                    OracleDBOperation oracleDBOperation = new OracleDBOperation(strConn);
                    ds = oracleDBOperation.GetResult(strSQL); break;
                case "1":
                    OdbcDBOpration odbcDBOpration = new OdbcDBOpration(strConn);
                    ds = odbcDBOpration.GetResult(strSQL); break;
                default:
                    OracleDBOperation oracleDBOperationDefault = new OracleDBOperation(strConn);
                    ds = oracleDBOperationDefault.GetResult(strSQL); break;
            }
            return ds;
        }

        /// <summary>
        /// 更新数据库.
        /// </summary>
        /// <param name="strSQL">SQL语句</param>
        /// <returns>是否成功</returns>
        public bool UpdateData(string strSQL)
        {
            bool blnUpdateData = false;

            try
            {
                switch (strDBTYPE)
                {
                    case "0":
                        OracleDBOperation oracleDBOperation = new OracleDBOperation();
                        blnUpdateData = oracleDBOperation.UpdateData(strSQL); break;
                    case "1":
                        OdbcDBOpration odbcDBOpration = new OdbcDBOpration();
                        blnUpdateData = odbcDBOpration.UpdateData(strSQL); break;
                    default:
                        OracleDBOperation oracleDBOperationDefault = new OracleDBOperation();
                        blnUpdateData = oracleDBOperationDefault.UpdateData(strSQL); break;
                }
            }
            catch (Exception ex)
            {
                DBException dbe = new DBException();
                dbe.SaveException(ex, "Creat new OracleDBOperation erro.SQL=" + strSQL);
            }
            finally { }

            return blnUpdateData;
        }

        /// <summary>
        /// 更新指定连接的数据库
        /// </summary>
        /// <param name="strSQL">SQL语句</param>
        /// <param name="strConn">数据库连接</param>
        /// <returns>是否成功</returns>
        public bool UpdateDataFromConn(string strSQL, string strConn)
        {
            bool blnUpdateData = false;

            try
            {
                switch (strDBTYPE)
                {
                    case "0":
                        OracleDBOperation oracleDBOperation = new OracleDBOperation(strConn);
                        blnUpdateData = oracleDBOperation.UpdateData(strSQL); break;
                    case "1":
                        OdbcDBOpration odbcDBOpration = new OdbcDBOpration(strConn);
                        blnUpdateData = odbcDBOpration.UpdateData(strSQL); break;
                    default:
                        OracleDBOperation oracleDBOperationDefault = new OracleDBOperation(strConn);
                        blnUpdateData = oracleDBOperationDefault.UpdateData(strSQL); break;
                }
            }
            catch (Exception ex)
            {
                DBException dbe = new DBException();
                dbe.SaveException(ex, "Creat new OracleDBOperation erro.SQL=" + strSQL + ".Conn=" + strConn);
            }
            finally { }

            return blnUpdateData;
        } 
    }
}
